package com.xzlf.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xzlf.domain.TFile;
import com.xzlf.service.NbsScheduled;
import com.xzlf.service.TFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;


@Component
@Slf4j
public class PrintTask implements NbsScheduled {

    @Autowired
    private TFileService tFileService;

    @Value("${server.name}")
    private String serverName;

    @Override
    public void execute() {

        log.info("我是：{}，抢到了锁，使用线程：{}，开始执行任务：task_2", serverName, Thread.currentThread().getName());
        TFile one = tFileService.getOne(new QueryWrapper<TFile>()
                .select("id", "name")
                .likeRight("name", "task_2_")
                .orderByDesc("id").last("limit 1"));
        String name = one == null ? null : one.getName();
        if (name == null) {
            name = "task_2_001";
        } else {
            int i = Integer.parseInt(name.substring(name.lastIndexOf('_') + 1));
            name = "task_2_" + String.format("%03d", (i + 1));
        }
        System.out.println("=================" + name);
        TFile tFile = new TFile(name, "task_2", "create By " + serverName);
        tFileService.save(tFile);
        log.info("我是：{}，完成了任务task_2。", serverName);
    }
}
